package cmcc.dbi.controller;

import cmcc.dbi.dto.*;
import cmcc.dbi.entity.InspectionItemRecord;
import cmcc.dbi.result.Result;
import cmcc.dbi.service.InspectionRemoteConnService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jcraft.jsch.JSchException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/inspection/remote")
@Api(tags = "远程连接目标主机数据库进行巡检")
@Slf4j
public class InspectionRemoteConnController {
    @Autowired
    InspectionRemoteConnService inspectionRemoteConnService;

    @ApiOperation("获取远程数据库的巡检信息")
    @PostMapping("/getSqlResult")
    public Result getSqlResult(@RequestBody RemoteDbRequestQueryDTO remoteDbRequestQueryDTO) throws SQLException {
        List<InspectionItemRecord> scriptLists = remoteDbRequestQueryDTO.getScriptLists();
        DbQueryDTO dbQueryDTO = remoteDbRequestQueryDTO.getDbQueryDTO();

        return Result.success(inspectionRemoteConnService.getRemoteSqlResult(scriptLists, dbQueryDTO));
    }

    @ApiOperation("获取远程主机的巡检信息")
    @PostMapping("/getShellResult")
    public Result getShellResult(@RequestBody RemoteShellRequestQueryDTO remoteShellRequestQueryDTO) throws JSchException {
        List<InspectionItemRecord> scriptLists = remoteShellRequestQueryDTO.getScriptLists();
        ShellQueryDTO shellQueryDTO = remoteShellRequestQueryDTO.getShellQueryDTO();

        return Result.success(inspectionRemoteConnService.getRemoteShellResult(scriptLists, shellQueryDTO));
    }

    @ApiOperation("获取巡检结果")
    @PostMapping("/getInspectedResult")
    public Result getInspectedResult(@RequestBody RemoteRequestQueryDTO remoteRequestQueryDTO) throws SQLException {
        JSONArray sqlJsonArray = new JSONArray();
        List<InspectionItemRecord> sqlScriptLists = remoteRequestQueryDTO.getSqlScriptLists();
        DbQueryDTO dbQueryDTO = remoteRequestQueryDTO.getDbQueryDTO();

        JSONArray shellJsonArray = new JSONArray();
        List<InspectionItemRecord> shellScriptLists = remoteRequestQueryDTO.getShellScriptLists();
        ShellQueryDTO shellQueryDTO = remoteRequestQueryDTO.getShellQueryDTO();

        if(sqlScriptLists.size() > 0){
            sqlJsonArray = inspectionRemoteConnService.getRemoteSqlResult(sqlScriptLists,dbQueryDTO);
        }
        if(shellScriptLists.size() > 0){
            try {
                shellJsonArray = inspectionRemoteConnService.getRemoteShellResult(shellScriptLists, shellQueryDTO);
            } catch (JSchException e) {
                log.error("shell脚本执行出现问题 -> {}", e.getMessage());
            }
        }

        JSONObject mergeJson = new JSONObject();
        mergeJson.put("sql_result", sqlJsonArray);
        mergeJson.put("shell_result", shellJsonArray);

        return Result.success(mergeJson);
    }

}
